<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['lang']){
	$_SESSION['show_lang']=$_RQ['lang'];
}
model('jy_weishop/member')->checklogin();
if($_RQ['op']=='main'){
	$teamid = intval($_RQ['teamid']);
	$this->session('teamid',$teamid);
	$return['teamid']=$teamid;
	$shopset =plugin('jy_weishop')->plugin_setting('shopset');
	//营销设置
	$saleset=plugin('jy_weishop')->plugin_setting('saleset');
	$member=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$member2=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('avatar','nickname','credit1','credit2'));
	$member=array_merge($member,$member2);
	$goodsid = intval($_RQ['goodsid']);
	//来自商户的订单
	$merchid = intval($_RQ['merchid']);
	if($_RQ['storeid']){
		$_SESSION['store_id']=intval($_RQ['storeid']);
	}
	$store_id=$_SESSION['store_id'];
	//返回链接
	$return['forward_url']=base64_encode('jy_weishop/order.list//fromcreate=1');
	//下单页
	$item=array();
	//提交数据
	$return['post'] = array();
	if(!$merchid){
		$carrier_list = pdo_getall('jy_weishop_store',array('uniacid' =>$_SESSION['uniacid'],'status'=>1,'type'=>array(1,3)),'','','paixu desc,id desc');
		if($carrier_list){
			foreach($carrier_list as &$pv){
				$pv['pcd']=json_decode($pv['pcd'],true);
			}
			$return['carrier_list']=$carrier_list;
			$return['selectstore']=$carrier_list['0'];
		}
	}
	if($goodsid){
		$_SESSION['goodsid']=$goodsid;
		//直接购买。
		$return['post']['goodsid'] = $goodsid;
		$goodsfile=array('id','isdiyprice','groupprice','diyprice','invoice','type','buylevels','buygroups','title','merchid','status','pcate','total','subtitle','image','video','price','totalcnf','weight','active_plugin','active_id','storeids','salenum','pluginname','originalprice','usermaxbuy','minbuynum','unit','isverify','diyfields');
		$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid),$goodsfile);
		if(!$goods){
			exi('指定商品不存在！','error','goback');
		}
		if($_SESSION['is_tomedia']){
		$goods['image']=$goods['image']?tomedia($goods['image']):'';
		}
		$diyfields=model('jy_weishop/goods')->get_diyformfields($goodsid);
		//配送设置
		//配送设置
		$goodsdispatch=model('jy_weishop/goods')->get_dispatch($goodsid);
		$goods=array_merge($goods,$goodsdispatch);
		//如果来自其他商户分销（修改商户ID）
		$frommerchid=$this->session('frommerchid');
		$goods['merchid']=$frommerchid?$frommerchid:$goods['merchid'];
		model('jy_weishop/goods')->check_buy($goods['id'],intval($_RQ['num']),intval($_RQ['optionsid']));
		//判断距离
		if($goods['merchid']){
			$merset=pdo_getcolumn('core_account_plugin_setting',array('uniacid'=>$_SESSION['uniacid'],'key'=>'peisong'.$goods['merchid'],'plugin'=>'jy_merch'),'data');
			$juli=json_decode($merset,true);
			$juli=$juli['juli'];
			$return['juli']=$juli;
		
		}
		//购买权限：
		$levelinfo = model('jy_weishop/member')->get_level();
		if($goods['buylevels']){
			$fastvalue=substr($goods['buylevels'],0,1);
			if($fastvalue!=';'){
				$goods['buylevels']=';'.$goods['buylevels'];
			}
			$lastvalue=substr($goods['buylevels'],-1,1);
			if($lastvalue!=';'){
				$goods['buylevels']=$goods['buylevels'].';';
			}
			if(strpos($goods['buylevels'],';'.$levelinfo['id'].';')===false){
				exi('您没有权限购买该商品！','error','index/');
			}
		}
		$groupinfo = model('jy_weishop/member')->get_group();
		if($goods['buygroups']){
			$fastvalue=substr($goods['buygroups'],0,1);
			if($fastvalue!=';'){
				$goods['buygroups']=';'.$goods['buygroups'];
			}
			$lastvalue=substr($goods['buygroups'],-1,1);
			if($lastvalue!=';'){
				$goods['buygroups']=$goods['buygroups'].';';
			}
			if(strpos($goods['buygroups'],';'.$groupinfo['id'].';')===false){
				exi('您没有权限购买该商品！','error','index/');
			}
		}
		$goods['goodsid']=$goods['id'];
		$optionsid = intval($_RQ['optionsid']);
		$goods['storeid']=$store_id;
		$goods['optionid']=$optionsid;
		$goods['roomid']=intval($_RQ['roomid']);
		$goods=model('jy_weishop/activity')->get_upgoods($goods);
		//重置下单数量
		$goods['goodstotal']=$goods['total'];
		$goods['total']=intval($_RQ['num'])?:1;
		if($optionsid){
			$return['post']['optionid'] = $optionsid;
			if(!$goods['options']){
					exi('指定规格的商品不存在！','error','goback');
			}
			$specs = explode("_", $goods['options']['specs']);
			$specsall = pdo_getall('jy_weishop_goods_spec_item',array('id'=>$specs,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
			if($specsall){
					$optiontitle='';
					foreach($specsall as $k=>&$sp){
						if($k==0){
							$optiontitle .=$sp['title'];
						}else{
							$optiontitle .='，'.$sp['title'];
						}
					}
			}
			$goods['optiondata']= $optiontitle;
		}else{
			$goods['optionid']=0;
			$return['post']['optionid'] = 0;
		}
		if($goods['active_plugin'] && !$optionsid){
				//商品在做活动，检查下库存是否不足
				model('jy_weishop/activity')->check_stock($goods['id'],$goods['active_plugin'],$goods['active_id'],$goods['total']);
		}
		$return['post']['total'] = intval($_RQ['num']);
		$return['post']['invoice'] = $goods['invoice'];
		if($goods['type']==1 || $goods['type']==2){
			//虚拟商品
			$order['sendtype']=3;
		}elseif($goods['type']==3){
			//计时计次服务，发货类型必须是上门核销
			$order['sendtype']=2;
		}elseif($goods['type']==0 || $goods['isverify']){
			//实体商品，查找是否有门店，如果有门店则表示支持自提，否则只能是快递配送
			if(!$carrier_list){
				$order['sendtype']=1;
			}else{
				$return['sendtypes']=array(
					'1'=>'快递配送',
					'2'=>'自提',
				);
			}
		}
		if($goods['groupprice']==0){
				$return['pluginset']['groupdiscount']=$return['pluginset']['groupdiscount']?:98;
				$goods['groupprice'] =$goods['price']*$return['pluginset']['groupdiscount']/100;
		}
		$order=array(
			'sendtype'=>$order['sendtype'],
			'type'=>'one',
			'list'=>array($goods),
		);
	}else{
		//从购物车购买
		exi('拼单类型，不能从购物车下单！','error','jy_pingdd/index/');
	}
	if(empty($order['list'])){
		exi('请先选择商品再下单！','error','jy_pingdd/index/');
	}
	//统计订单商品总数和总价
	$total=0;
	$total_price=0.00;
	$total_weight=0;
	foreach($order['list'] as &$ov){
		if($ov['merchid']){
			$isid=pdo_getcolumn('jy_merch_user',array('id'=>$ov['merchid'],'uniacid'=>$_SESSION['uniacid']),'id');
			if(!$isid){
				$order['exitnomerch']=1;
			}
		}else{
			$order['exitnomerch']=1;
		}
		$total+=$ov['total'];
		$total_price+=$ov['total']*$ov['groupprice'];
		$total_weight+=$ov['weight']*$ov['total'];
	}
	$order['total']=$total;
	$order['total_price']=$total_price;
	$order['total_weight']=round(($total_weight/1000)*1000,2);
	//计算运费
	$item['address']=array();
	$order['dispatchprice']=0;
	$order['sendtype']=$order['sendtype']?:1;
	if(!$order['sendtype'] || $order['sendtype']==1 || $return['sendtypes']){
		if(intval($_RQ['addressid'])){
			$item['address']=pdo_get('core_address',array('mid'=>$_SESSION['uid'],'id'=>intval($_RQ['addressid'])));
			$item['addressid']=intval($_RQ['addressid']);
		}else{
			$member_address=pdo_getall('core_address',array('mid'=>$_SESSION['uid']));
			if($member_address){
				foreach($member_address as &$address){
					if($address['isdefault']){
						$item['addressid']=$address['id'];
						$item['address']=$address;
						break;
					}
				}
				if(!$item['addressid']){
					$item['addressid']=$member_address['0']['id'];
					$item['address']=$member_address['0'];
					
				}
				
			}
		}
		$city_express=plugin('jy_weishop')->plugin_setting('citypeisong');
		if($item['address']){
			$item['address']['pcd']=json_decode($item['address']['pcd'],true);
			try{
				if($city_express['isopen']){
					$addressinfo=$item['address']['pcd']['province'] . $item['address']['pcd']['city'] . $item['address']['pcd']['district'] . $item['address']['pcd']['street'] . $item['address']['address'];
					$get_freight=model('jy_weishop/order/dispatch')->get_peisong($addressinfo,$_SESSION['store_id'],$item['address']['id'],$order['total_price']);
					if($get_freight['is_dispatch']==1){
						$get_freight=model('jy_weishop/order/dispatch')->get_freight($order['list'],$item['address']['pcd'],true,true);
					}else{
						$order['is_peisong']=1;
						$order['peisong_juli']=price($get_freight['juli']);
						//不在同城,不能下单！
						if($get_freight['iscity']==0){
							$order['outarea']=1;
						}
					}
				}else{
					$get_freight=model('jy_weishop/order/dispatch')->get_freight($order['list'],$item['address']['pcd'],true,true);
					if($get_freight['outarea']){
							$order['outarea']=1;
					}
				}
				$order['dispatchprice']=$get_freight['fee'];
				$order['isonebaoyou']=$get_freight['isonebaoyou'];
				$order['addbaoyoufee']=$get_freight['addbaoyoufee'];
				$order['baoyou_man']=$get_freight['baoyou_man']?:0;
			}catch(Exception $e){
				unset($item['address'],$item['addressid']);
				$order['dispatchprice']=0;
				$item['isnoarea']=1;
			}
		}else{
			if($city_express['isopen']){
					$get_freight=model('jy_weishop/order/dispatch')->get_peisong('',$_SESSION['store_id'],'',$order['total_price']);
					$order['is_peisong']=1;
					$order['peisong_juli']=price($get_freight['juli']);
			}else{
				$get_freight=model('jy_weishop/order/dispatch')->get_freight($order['list'],'',true,true);
			}
			$order['dispatchprice']=$get_freight['fee'];
			$order['isonebaoyou']=$get_freight['isonebaoyou'];
			$order['addbaoyoufee']=$get_freight['addbaoyoufee'];
			$order['baoyou_man']=$get_freight['baoyou_man']?:0;
		}
		//多少钱起送
		$order['no_out_price']=$get_freight['no_out_price']?:0;
	}
	
	//会员折扣价
	$order['level_de_price']=0.00;
	if(!$order['no_member_discount']){
		//允许使用会员折扣，整理会员折扣数据
		$member_levelid=pdo_getcolumn('jy_weishop_member',array('mid'=>$_SESSION['uid']),'level');
		if($member_levelid){
			$member_level=pdo_get('jy_weishop_member_level',array('id'=>$member_levelid));
			if($member_level){
				$member_level['discount']=$member_level['discount']>0.1?$member_level['discount']:10;
				$order['level_de_price']=$order['total_price']-$order['total_price']*($member_level['discount']/10);
				$order['level_de_price']=round($order['level_de_price'],2);
			}
		}
	}
	//运势折扣价
	$order['luck_de_price']=0.00;
	$sale_luck=plugin('jy_weishop')->plugin_setting('sale_luck');
	$myluckkey=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'luckkey');
	if($myluckkey && $sale_luck['isopen']){
		$discount=$sale_luck['lucklist'][$myluckkey]['discount'];
		$discount=($discount>10&&$discount<=100)?$discount:100;
		$order['luck_de_price']=$order['total_price']-($order['total_price']*$discount/100);
	}
	//满减优惠价格：
	$sale_enough=$saleset['enough'];
	if($sale_enough){
		foreach($sale_enough as $k=>$ev){
			if($ev['man']>$order['total_price']){
				unset($sale_enough[$k]);
			}
		}
		paixu($sale_enough,'man','desc');
	}
	$order['sale_enough']=$sale_enough;
	$order['enough_de_price']=$sale_enough[0]['jian']?$sale_enough[0]['jian']:0.00;
	//送积分
	$buy_credit=plugin('jy_weishop')->plugin_setting('buy_credit');
	if($buy_credit['open']){
		$order['credit']=0;
		if($buy_credit['type']==1){
			foreach($buy_credit['options'] as $buyv){
				if($buyv['start']<$order['total_price']){
					$credit=$buyv['credit']*$order['total_price'];
					$order['credit']=$order['credit']<$credit?$credit:$order['credit'];
				}
			}
		}else{
			foreach($order['list'] as $cv){
				$credit=pdo_getcolumn('jy_weishop_goods_credit',array('goods_id'=>$cv['goodsid'],'uniacid'=>$_SESSION['uniacid']),'credit');
				$credit=$credit?:0;
				$order['credit'] +=$credit;
			}
		}
		paixu($saleset['enough'],'man','desc');
	}
	//积分抵扣
	if($saleset['deduct_credit1']){
		$order['deduct_credit']=$member['credit1']/$saleset['deduct_bili'];
		if($order['deduct_credit']>$order['total_price']/2){
			$order['deduct_credit']=price($order['total_price']/2,true);
		}
		$order['delcredit1']=intval($order['deduct_credit']*$saleset['deduct_bili']);
	}
	//优惠券优化价格：
	$order['coupon_de_price']=0.00;
	if(!$order['no_coupon_discount']){
		//整理优惠券数据，先获取用户所有待使用优惠券，再从中筛选出未过期的优惠券且本次下单可用的优惠券！
		$newcoupons=model('jy_weishop/coupon')->getmycoupon_ing(1,$order['total_price'],$order['level_de_price'],$order['list']);
		$order['coupon_de_price']=$newcoupons[0]['de_price'];
		$item['couponid']=$newcoupons[0]['id'];
		$return['couponcount']=count($newcoupons);
	}
	$return['coupons']=$newcoupons?$newcoupons:array();
	//整理收货地址信息
	if($store_id){
		$return['selectstore']=pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$store_id));
	}
	if($return['selectstore']['id']){
		$return['selectstore']['juli']=1;
	}
	$return['address_list']=pdo_getall('core_address',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$return['address_item']=array();
	$return['citypeisong']=$city_express;
	//提交数据
	$return['post']['merchid'] = $merchid;
	$return['post']['couponid'] = $newcoupons[0]['id'];
	$return['post']['make_type'] = 0;
	$return['post']['dayid'] = 0;
	$return['post']['title5'] = $newcoupons[0]['coupon']['title5'];
	$return['post']['deduct'] = $newcoupons[0]['coupon']['deduct'];
	$return['post']['discount'] = $newcoupons[0]['coupon']['discount'];
	$return['post']['backtype'] = $newcoupons[0]['coupon']['backtype'];
	$return['post']['addressid'] = $item['addressid'];
	$return['post']['sendtype'] = $order['sendtype']?:1;
	$return['post']['sendtime'] = strtotime("+2 day");
	$return['post']['storeid'] = $return['selectstore']['id'];
	$return['post']['deliverytime'] = date("m月d日",strtotime("+2 day"));
	$return['post']['diyfields'] = $diyfields;
	$return['post']['dispatchprice']=$order['dispatchprice'];
	//配送
	$order['deliverytime'] = $return['post']['deliverytime'];
	//整理最终价(暂不扣除优惠券)post是最终价，order是未减优惠券的价
	$order['endprice']=round($order['total_price'] + $order['dispatchprice'] - $order['level_de_price'] - $order['luck_de_price']  - $order['enough_de_price'] - $order['deduct_credit'],2);
	$order['no_dispatch_price']=round($order['total_price'] - $order['level_de_price'] - $order['luck_de_price'] - $order['enough_de_price'] - $order['deduct_credit'],2);
	$order['endprice']=$order['endprice']>=0?$order['endprice']:0;
	$return['post']['endprice']=$order['endprice']- $order['coupon_de_price'];
	$return['post']['endprice']=$return['post']['endprice']>=0?$return['post']['endprice']:0;
	$return['post']['no_dispatch_price']=$order['no_dispatch_price']- $order['coupon_de_price'];
	$return['post']['no_dispatch_price']=$return['post']['no_dispatch_price']>=0?$return['post']['no_dispatch_price']:0;
	/*
		赠品
		先根据订单金额找赠品，再根据商品找赠品
	*/
	$goods_gifts=pdo_getall('jy_weishop_goods_gift',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'activity'=>0));
	//$current_gift=array();
	if($goods_gifts){
		foreach($goods_gifts as $gift){
			$gift['onlinetime']=json_decode($gift['onlinetime'],true);
			if($gift['onlinetime']['tstart']<time() && $gift['onlinetime']['tend']>time() && $gift['orderprice'] && $gift['orderprice']<$order['no_dispatch_price']){
				if(!$current_gift || $current_gift['orderprice']<$gift['orderprice']){
					$current_gift=$gift;
				}
			}
		}
	}
	if(!$current_gift){
		$goods_gifts=pdo_getall('jy_weishop_goods_gift',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'activity'=>1));
	}
	if($goods_gifts){
		foreach($goods_gifts as $gift){
			$gift['onlinetime']=json_decode($gift['onlinetime'],true);
			if($gift['onlinetime']['tstart']<time() && $gift['onlinetime']['tend']>time() && $gift['goodsid']){
				foreach($order['list'] as $goods){
					if($goods['goodsid']==$gift['goodsid']){
						$current_gift=$gift;
						break;
					}
				}
			}
			if($current_gift){
				break;
			}
		}
	}
	if($current_gift){
		$order['gift_id']=$current_gift['id'];
		$return['gifts']=json_decode($current_gift['gifts'],true);
		foreach($return['gifts'] as &$gv){
			$gv['price']=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['id'],'uniacid'=>$_SESSION['uniacid']),'price');
			if($_SESSION['is_tomedia']){
				$gv['image']=$gv['image']?tomedia($gv['image']):'';
			}
		}
	}
	$order['total_price']=round($order['total_price'],2);
	$_SESSION['order']=$return['order']=$order;
	$item['sendtype']=1;
	$return['item']=$item;
	//获取行政数据
	$return['province']=cfc('tools')->pcd();
	//自提点
	
	set_html('title','确认订单');
	//代付
	$peerpay_setting=plugin('jy_weishop')->plugin_setting('peerpay');
	if($peerpay_setting && $peerpay_setting['open'] && $order['endprice']>0){
		$return['peerpay']=true;
	}
	//获取小程序订阅消息ID
	if($_SESSION['wxapp_id']){
		$tmpid=cfc('wxapp_api')->get_tpl_template_id('3534',[1,2,3,4],'pay2buyer','订单支付成功通知','jy_weishop');
		if($tmpid){
			$tmplIds[]=$tmpid;
		}
		$tmpid=cfc('wxapp_api')->get_tpl_template_id('3534',[1,2,3,4],'send2buyer','订单发货通知','jy_weishop');
		if($tmpid){
			$tmplIds[]=$tmpid;
		}
		$tmpid=cfc('wxapp_api')->get_tpl_template_id('3534',[1,2,3,4],'receive2buyer','订单确认收货通知','jy_weishop');
		if($tmpid){
			$tmplIds[]=$tmpid;
		}
		$return['tmplIds']=$tmplIds;
	}
	exi($return);
}

if($_RQ['op']=='juli'){
	//判断距离
		$post=get_postdata();
		$merchid=pdo_getcolumn('jy_weishop_goods',array('id'=>$_SESSION['goodsid']),'merchid');
		$l=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid),array('lat','lng'));
		if($l['lng'] && $l['lat']){
			$d=cfc('tools')->getdistance($post['lat_lng']['lng'],$post['lat_lng']['lat'],$l['lng'],$l['lat']);
			if($d/1000>$post['juli']){
				exi('超出配送距离,无法购买','error','/goods.detail//id='.$_SESSION['goodsid']);
				exi('success');
			}else{
				exi('success');
			}
		}	
}
// 完成下单
if($_RQ['op']=='post'){
	$this->model('model')->checkteam($_SESSION['goodsid']);
	$post=get_postdata();
	//整理订单数据
	$order=$_SESSION['order'];
	$teamid = $this->session('teamid');
	//公共参数
	if($post['out_time']){
		//配送时间
		$days = array(
				0=>date("Y-m-d",time("day")),
				1=>date("Y-m-d",strtotime("+1 day")),
				2=>date("Y-m-d",strtotime("+2 day")), 
				3=>date("Y-m-d",strtotime("+3 day")), 
				4=>date("Y-m-d",strtotime("+4 day")),
				5=>date("Y-m-d",strtotime("+5 day")),
				6=>date("Y-m-d",strtotime("+6 day")),
		);
		$dayid=intval($post['dayid']);
		$post['out_time']=$days[$dayid].' '.$post['out_time'];
	}
	$new_order=array(
		'mid'=>$_SESSION['uid'],
		'goodsprice'=>$order['total_price'],
		'remark'=>trim($post['remark']),
		'goods'=>$order['list'],
		'merchid'=>$order['merchid'],
		'sendtype'=>intval($post['sendtype'])?:1,
		'diyfields'=>ijson_encode($post['diyfields']),
		'out_time'=>trim($post['out_time']),
		'make_type'=>intval($post['make_type']),
		'is_peisong'=>$order['is_peisong'],
	);
	$new_order['invoce']=array(
		'type'=>$post['invoicetype'],
		'goodstype'=>$post['invoicegoodstype'],
		'name'=>$post['invoicename'],
		'code'=>$post['invoicecode'],
		'email'=>$post['email'],
	);
	//会员折扣
	if(!$order['no_member_discount'] && $order['level_de_price']){
		$new_order['level_de_price']=$order['level_de_price'];
	}
	//运势折扣
	if($order['luck_de_price']){
		$new_order['luck_de_price']=$order['luck_de_price'];
	}
	//满减
	$new_order['enough_de_price']=$order['enough_de_price'];
	//抵扣
	$new_order['deduct_credit']=$order['deduct_credit']?:0;
	$new_order['price']=$new_order['goodsprice']-intval($new_order['enough_de_price'])-$new_order['level_de_price']-$new_order['luck_de_price']-$new_order['deduct_credit'];
	//优惠券
	if(!$order['no_coupon_discount']){
		$couponid=intval($post['couponid']);
		//获取优惠信息
		$getcouponfee=model('jy_weishop/coupon')->getorderfee($couponid,$new_order['price']);
		if($getcouponfee){
			$new_order=array_merge($new_order,$getcouponfee);
		}
	}
	//自动分配门店
	$new_order['storeid']=intval($post['storeid']);
	if($new_order['sendtype']==1){
		//快递配送
		$new_order['addressid']=intval($post['addressid']);
		if(!$new_order['addressid']){
			exi('请先选择收货地址！','error');
		}
		$pcd=pdo_getcolumn('core_address',array('id'=>$new_order['addressid']),'pcd');
		if($pcd){
			$pcd=json_decode($pcd,true);
			$new_order['dispatchprice']=$post['dispatchprice'];
			$new_order['price']+=$new_order['dispatchprice'];
		}		
		if($post['juli']){
			$merchid=pdo_getcolumn('jy_weishop_goods',array('id'=>$_SESSION['goodsid']),'merchid');
			$l=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid),array('lat','lng'));
			if($l['lng'] && $l['lat']){
				$d=cfc('tools')->getdistance($post['lat_lng']['lng'],$post['lat_lng']['lat'],$l['lng'],$l['lat']);
				if($d/1000>$post['juli']){
					exi('超出配送距离,无法购买','error','/goods.detail//id='.$_SESSION['goodsid']);
				}
			}
		}		
	}elseif($new_order['sendtype']==2){
		//门店自提
		if(!$new_order['storeid']){
			exi('请先选择门店！','error');
		}
	}elseif($new_order['sendtype']==3){
		//虚拟发货
	}
	$new_order['price']=$new_order['price']>0?$new_order['price']:0;
	if($new_order['price']<0){
		exi('订单金额不能小于零！','error');
	}
	if($order['gift_id']){
		$new_order['gift_id']=$order['gift_id'];
	}
	//拼单部分
	$new_order['teamid']=$teamid;
	$new_order['is_team']=1;
	$new_order['plugin_name']='jy_pingdd';
	$order_id=model('jy_weishop/order')->create($new_order);
	unset($_SESSION['order']);
	$params=array(
		'id'=>$order_id,
		'title'=>'购买商品',
		'fee'=>$new_order['price'],
		//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
		'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
		'back_url'=>'jy_pingdd/order.create/payresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
		'success_url'=>'jy_pingdd/myteam/',
		'error_url'=>'jy_pingdd/myteam/',
	);
	$payid=cfc('pay')->create_pay_order($params);
	pdo_update('jy_weishop_order',array('pay_id'=>$payid),array('id'=>$order_id));
	if($post['peerpay']){
		$peerpay_setting=plugin('jy_weishop')->plugin_setting('peerpay');
		if($peerpay_setting && $peerpay_setting['open']){
			exi('','','jy_weishop/order.peerpay//orderid='.$order_id);
		}
	}
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$order_id,
	);
	exi($res);
}
if($_RQ['op']=='get_freight'){
	$addressid=intval($_RQ['addressid']);
	$addressid=$addressid?:intval($_RQ['address']);
	if(!$addressid){
		exi('请先选择地址！','error');
	}
	$item=pdo_get('core_address',array('id'=>$addressid));	
	if(!$item['pcd']){
		exi('0');
	}
	$item['pcd']=json_decode($item['pcd'],true);
	$city_express=plugin('jy_weishop')->plugin_setting('citypeisong');
	if($city_express['isopen']){
		$addressinfo=$item['pcd']['province'] . $item['pcd']['city'] . $item['pcd']['district'] . $item['pcd']['street'] . $item['address'];
		$freight=model('jy_weishop/order/dispatch')->get_peisong($addressinfo,$_SESSION['store_id'],$item['id'],$_SESSION['order']['total_price']);
		if($freight['is_dispatch']==1){
			$freight=model('jy_weishop/order/dispatch')->get_freight($_SESSION['order']['list'],$item['address']['pcd'],true,true);
		}else{
			//不在同城,不能下单！
			if($freight['iscity']==0){
				$freight['outarea']=1;
			}
		}
	}else{
		$freight=model('jy_weishop/order/dispatch')->get_freight($_SESSION['order']['list'],$item['pcd'],'',true);
		if($freight['outarea']){
			$freight['outarea']=1;
		}
	}
	//多少钱起送
	$freight['no_out_price']=$freight['no_out_price']?:0;
	if($_RQ['showbaoyou']){
		$freight['baoyou_man']=$freight['baoyou_man']?:0;
		exi($freight);
	}else{
		exi($freight['fee']);
	}
	
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_weishop/order.list/');
	}
	$order=pdo_get('jy_weishop_order',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_weishop/order.list/');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$payresult['fee'],'error','jy_weishop/order.list/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
		'status'=>10,
	);
	model('jy_weishop/order')->order_pay($order['id'],$params);
	$goodsid=pdo_getcolumn('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['id']),'goodsid');
	if(!$order['teamid']){//非参团
			$insertdata=array(
						'uniacid'=>$_SESSION['uniacid'],
						'goodsid'=>$goodsid,
						'mid'=>$_SESSION['uid'],
						'plugin_name'=>'jy_pingdd',
						'createtime'=>TIMESTAMP
			);
			pdo_insert('jy_weishop_groups_team',$insertdata);
			$teamid=pdo_insertid();
			pdo_update('jy_weishop_order',array('teamid'=>$teamid),array('id'=>$order['id']));
	}
	//拼单部分
	$num=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>10,'teamid'=>$order['teamid']));
	$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
	$groupnum=$goods['groupnum']?:2;
	if($groupnum<=$num){
		pdo_update('jy_weishop_groups_team',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['teamid']));
		$data=array(
			'status'=>1,
			'groupfinishtime'=>TIMESTAMP,
		);
		$orders=pdo_getall('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'teamid'=>$order['teamid'],'status'=>'10'));
		foreach($orders as $item){
			pdo_update('jy_weishop_order',$data,array('id'=>$item['id']));
			if($goods['isverify'] || $goods['type']!=0){
				model('jy_weishop/order')->send_orderverify($item['id']);
			}
			$item['groupfinishtime']=TIMESTAMP;
			$item['goodstitle']='拼单-'.$goods['title'];
			model('jy_weishop/notice')->notice_groupfinish($item,$url);
		}
	}
	exi('提交成功!');
}

?>